<template>
  <el-dialog title="分配角色" :visible="showRoleDialog" @close="cancel">
    <!-- 多选框组 -->
    <el-checkbox-group v-model="roleIds">
      <!-- label属性是该 checkbox 对应的value值，若标签中无内容，则该属性也充当 checkbox 按钮后的介绍 -->
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{
        item.name
      }}</el-checkbox>
    </el-checkbox-group>
    <!-- 按钮 -->
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" type="primary" @click="submit">确定</el-button>
        <el-button size="small" @click="cancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetailByIdAPI } from '@/api/user'
import { assignRole } from '@/api/employees'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      required: true
    },
    userId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: [],
      roleIds: []
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      const { rows } = await getRoleList()
      this.list = rows
    },
    async getUserDetail(id) {
      const { roleIds } = await getUserDetailByIdAPI(id)
      this.roleIds = roleIds
    },
    async submit() {
      try {
        await assignRole({ id: this.userId, roleIds: this.roleIds })
        this.$emit('update:showRoleDialog', false)
        this.$message.success('分配角色成功')
      } catch (error) {
        console.log(error)
      }
    },
    cancel() {
      this.$emit('update:showRoleDialog', false)
      this.roleIds = []
    }
  }
}
</script>

<style></style>
